package org.jpush.core;

import java.util.Iterator;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

/**
 * A <code>ConnectionGC</code> is a singleton
 * It is used to dispose the connection, it
 * is just like the JVM GC
 * 
 * @author Lei Wang
 *
 */
public class ConnectionGC
{
	private static final ConnectionGC gc = new ConnectionGC();
	
	public static ConnectionGC getGC()
	{
		return gc;
	}

	// The connection ready to dispose
	private Queue<LogicalConnection> waitingQueue = 
			new ConcurrentLinkedQueue<LogicalConnection>();

	private ConnectionGC() {}

	/**
	 * Remove the connection which can be deleted from the queue
	 */
	public void activate()
	{
		Iterator<LogicalConnection> it = waitingQueue.iterator();
		while (it.hasNext())
		{
			if (it.next().canDelete())
			{
				it.remove();
			}
		}
	}

	/**
	 * Add a connection ready to dispose into the queue
	 * 
	 * @param logicalConnection connection ready to dispose
	 */
	public void addDisposableClient(LogicalConnection logicalConnection)
	{
		waitingQueue.add(logicalConnection);
	}

	public void addDisposableMonitor(PhysicalConnection connection)
	{
		// Do nothing
	}
}
